<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <link rel="icon" href="*{/images/favicon.ico}" type="image/x-icon" />
  <meta http-equiv="P3P" content="CP='NOI NID'">
  <meta http-equiv="_broadcast_from_" Content="1" />
  <script type="text/javascript" src="*{/js/jquery.js}"></script>
  <script type="text/javascript" src="*{/js/func.js}"></script>
  <script type="text/javascript" src="*{/js/base64.js}"></script>
  <title>本地补丁同步</title>
<style type="text/css">
td,th{
	padding:3px;
}
.status_color_0 {
  /* 版本相同 */
  background-color:#FFFFFF;
}
.status_color_1 {
  /* 补丁没有，本地有 */
  background-color:#FFCC66;
}
.status_color_2 {
  /* 补丁有，本地没有 */
  background-color:#EFEFEF;
}
.status_color_3 {
  /* 补丁新，本地旧 */
  background-color:#FFFF00;
}
.status_color_4 {
  /* 补丁旧，本地新 */
  background-color:#FF99CC;
}
.bt_sync {
  background-color:#CCFF00;
}
.bt_sync_warning {
  background-color:#ff0000;
}
</style>
</head>
<body style="margin-left:10px;">
<div style="margin-top:10px;margin-bottom:20px;width:100%;text-align:center;">
  补丁管理平台
</div>
<p style="line-height:20px;"><a href="*{/N100029.ha}" target="_blank" style="border:solid 1px #AAAAAA;color:black;background: linear-gradient(#FFFFFF,#DDDDDD);padding:5px;text-decoration:none;">备份及当前全部脚本</a></p>
<div style="margin-bottom:20px;">
  <form id="upload_form">
    <span>上传及比对补丁：</span>
    <input type="file" name="ufile" style="border:solid 1px #CCCCCC;"/>
    <input type="button" value="提交" onclick="uploadPacage();"/>
  </form>
</div>
<div style="height:50px;margin-bottom:20px;">
  <div style="padding-left:100px;float:left;">
    <input type="button" value="只显示差异"  disabled id="bt_showonly" style="width:100px;margin-left:20px;background-color:#CCFF00;" onclick="javascript:changeShow(this);"/>
    <input type="button" value="全部同步"    disabled id="bt_sync_all" style="width:100px;margin-left:20px;background-color:#FF8C00;color:#000000;" onclick="javascript:syncAll();"/>
    <input type="button" value="生成补丁"    disabled id="bt_sync_3" style="width:100px;margin-left:20px;background-color:#00ffff;color:#000000;" onclick="javascript:createPackage();"/>
    <input type="button" value="反选"        disabled id="bt_sync_3_1" style="width:80px;margin-left:20px;background-color:#FFFFCC;color:#000000;" onclick="javascript:unSelectedCheckBox();"/>
    <input type="button" value="重置选择"    disabled id="bt_sync_3_2" style="width:80px;margin-left:20px;background-color:#FFFFCC;color:#000000;" onclick="javascript:resetCheckbox();"/>
  </div>
  <div style="clear:both;"></div>
</div>
<div id="no_change" style="text-align:center;display:none;">没有发生变化</div>
<div id="info_base">
  <div id="info_area"></div>
</div>
<form id="show_edit_form" method="POST" target="_blank" style="display:none;"></form>
<form id="create_package_form" method="POST" target="_blank" style="display:none;">
  <textarea name="infos" id="infos"></textarea>
</form>
<script type="text/javascript">
var maxLevel = 0;      //分类深度

/*
 * 信息包中的status
 *
 * 0 版本相同
 * 1 补丁有，本地没有
 * 2 补丁没有，本地有
 * 3 补丁新，本地旧
 * 4 补丁旧，本地新
 */

/**
 * 获取末尾分类说明
 */
function getLastPathTitle(title){
  var point = title.lastIndexOf("/");
  if(point<0){
    return title;
  }
  return title.substring(point+1,title.length);
}

/**
 * 将路径中的/转换为_
 */
function fixPathString(path){
  return path.replace(/\//g,"_");
}

/**
 * 点击显示本地脚本内容
 */
function showEditScript(node){
  var tr = $(node.parentNode.parentNode);
  var scriptId = tr.find("#script_id").text();
  var form = $("#show_edit_form"); //提交表单对象
  form.attr("action","*{/editor/index.htm}&id="+scriptId);
  form[0].submit();
  return true;
}

/**
 * 显示补丁中的脚本内容
 */
function showPackageScript(node){
  var tr = $(node.parentNode.parentNode);
  var scriptId = tr.find("#script_id").text();
  var form = $("#show_edit_form"); //提交表单对象
  form.attr("action","package_src.ha?id="+scriptId);
  form[0].submit();
  return true;
}

/**
 * 执行同步
 */
function executeSync(node){
	var tr = $(node.parentNode.parentNode);
    if(tr.find("#script_status").attr("status")=="4"){
      if(!confirm("当前补丁中，该脚本比本地的版本号旧，确认【补丁】中该脚本覆盖【本地】当前脚本？")){
        return;
      }
    }
	var scriptId = tr.find("#script_id").html();
	var res = getUrlValue("*{/N100106.ha}","id="+scriptId);
	var paras = "base64_enc=1&sync=1";
	if($(node).attr("title")){
		//如果再同步时报错了，第二次同步时只保存脚本文件并不编译和初始化
		alert("在同步报错后再次同步时，会只同步脚本，同步后并不启用该脚本，需要重启服务激活该脚本")
		paras += "&only_save=1";
	}
	var reXml = queryXml("*{/N100018.ha}&"+paras,Base64.toBase64(res));
    tr.find("#script_local_ver").html(tr.find("#script_package_ver").html());
	var errorMsg;
	if(reXml){
		errorMsg = reXml.find("error_msg").text();
	}else{
		errorMsg = "";
	}
	if(errorMsg){
		$(node).attr("title",errorMsg);
		tr.css("background-color","#FF0000");
		tr.find("#script_status").html("错误");
	}else{
		tr.css("background-color","#CCFF66");
		tr.find("#script_status").attr("status","0");
		tr.find("#script_status").html("相同");
	}
}


/**
 * 切换只显示有差异的脚本
 */
function changeShow(nod){
	var node = $(nod);
    var maxLevel = $("#max_level").val();
  
	if(node.attr("only_show")==1){
		node.val("只显示差异");
		node.attr("only_show","0");
		$("tr[script_line='1']").show();
		changeShowTable(true);
		changeShowType(true,maxLevel);
		$("#no_change").hide();
	}else{
		node.val("显示全部");
		node.attr("only_show","1");
		
		var scriptLines = $("tr[script_line='1']");
		var ele;
		for(var i=0;i<scriptLines.length;i++){
			ele = $(scriptLines[i]);
			if(ele.find("#script_status").attr("status")<2){
				ele.hide();
			}
		}
		changeShowTable(false);
		changeShowType(false,maxLevel);
		
		if($("[mark='type_block'][is_hide!='1']").length<1){
			$("#no_change").show();
		}
	}
}

function changeShowTable(show){
	var tables = $("[mark='script_table']");
	if(show){
		tables.show();
	}else{
		var table;
		for(var i=0;i<tables.length;i++){
			table = $(tables[i]);
			if(table.find("#script_status[status!='0'][status!='1']").length<1){
				table.hide();
			}
		}
	}
}


function changeShowType(show,level){
	var areas;
	if(show){
		areas = $("[mark='type_block']");
		areas.show();
		areas.attr("is_hide","0");
	}else{
		if(level<0){
			return;
		}
		areas = $("[mark='type_block'][level='"+level+"']");
		var area;
		for(var i=0;i<areas.length;i++){
			area = $(areas[i]);
			if(area.find("#script_status[status!='0'][status!='1']").length<1 
					&& area.find("#type_status[status!='0'][status!='1']").length<1){
				area.attr("is_hide","1");
				area.hide();
			}
		}
		changeShowType(show,level-1);
	}
}

/*
 * 全部同步
 */
function syncAll(){
	if(!confirm("确认【补丁】覆盖【本地】全部信息？")){
		return;
	}
	//处理分类信息
	var typeStatuses = $("[mark='type_status'][status!='0'][status!='1']");
	var btn;
	for(var i=0;i<typeStatuses.length;i++){
		btn = $(typeStatuses[i].parentNode.parentNode).find("#sbt_type_sync");
		executeSync(btn[0]);
	}
	//处理脚本信息
	var scriptStatuses = $("[mark='script_status'][status!='0'][status!='1']");
	for(var i=0;i<scriptStatuses.length;i++){
		btn = $(scriptStatuses[i].parentNode).find("#sbt_sync");
		executeSync(btn[0]);
	}
}


/**
 * 执行同步分类
 */
function executeTypeSync(node){
	var tr = $(node.parentNode.parentNode.parentNode);
  	var status = tr.find("#type_status").attr("status");
  	if(status=="4"){
      if(!confirm("当前补丁中，该信息比本地的版本号旧，确认【补丁】中信息本覆盖【本地】当前信息？")){
        return;
      }
    }
  
	var typePath = tr.find("#type_path").html();
	var status = tr.find("#type_status").attr("status");
	var res;
	var reXml;
	var paras;
  	//获取补丁路径信息
	res = getJson("*{/N100107.ha}","sub_path="+typePath);
    //去掉文件名
    var point = typePath.lastIndexOf("/");
    if(point>-1){
      typePath = typePath.substring(0,point);
    }else{
      typePath = "";
    }
	getJson("*{/N100006.ha}","ver="+res.ver+"&path="+typePath+"&aname="+res.aname+"&atype="+res.atype+"&explain="+urlEncodePro(res.explain));
		
	tr.find("#type_status").html("相同");
	tr.find("#type_status").attr("status","0");
		
	tr.find("#package_type_ver").html(tr.find("#local_type_ver").html());
	tr.find("#type_bt_area").hide();
	tr.attr("class","status_color_0");
  tr.find("#type_ver_area").hide();
}

function createPackage(){
  var form = $("#create_package_form");
  var values = "";
  $("[mark='type_package_check']").each(function(i){
    if(this.checked){
      values += "<path>"+$(this).val()+"</path>\n";
    }
  });
  $("[mark='script_package_check']").each(function(i){
    if(this.checked){
      values += "<file>"+$(this).val()+"</file>\n";
    }
  });
  form.find("#infos").val(values);
  form[0].action = "*{/N100108.ha}";
  form[0].submit();
}

function resetCheckbox(){
  $("[type='checkbox']").each(function(i){
    this.checked = false;
  });
}

function unSelectedCheckBox(){
  $("[type='checkbox']").each(function(i){
    this.checked = !this.checked;
  });
}

function hideShow(node){
 var pNode = $(node.parentNode.parentNode);
 node = $(node);
 if(node.attr("is_hide")=="1"){
   //显示
   node.attr("is_hide","0").css("background-color","#FFFFCC").val("隐藏");
   pNode.find("#type_area").show();
 }else{
   //隐藏
   node.attr("is_hide","1").css("background-color","#ff8000").val("显示");
   pNode.find("#type_area").hide();
 }
}

function unSelectedTableCheckBox(node){
  $(node.parentNode.parentNode).find("[type='checkbox']").each(function(i){
    this.checked = !this.checked;
  });
}
function resetTableCheckbox(node){
  $(node.parentNode.parentNode).find("[type='checkbox']").each(function(i){
    this.checked = false;
  });
}

function uploadPacage(){
	  submitForm(upload_form,"*{/N100103.ha}",function(res){
		  document.find("#info_area").html(res);
	  })
}

$("#bt_showonly")[0].disabled = false;
$("#bt_sync_all")[0].disabled = false;
$("#bt_sync_3")[0].disabled   = false;
$("#bt_sync_3_1")[0].disabled = false;
$("#bt_sync_3_2")[0].disabled = false;
</script>
</body>
</html>